<?php

/**
 * Login form decorator
 *
 */
class Application_Form_LoginDecorator extends Zend_Form_Decorator_Abstract 
{
    protected $_format;
    protected $_error;
    protected $_locale;
    
    public function __construct($options = null)
    {
        $this->_format='<div class="login-form-item">';
        $this->_format.='<div class="field-label"><label for="%s">%s</label></div>';
        $this->_format.='<div class="field"><input type="%s" name="%s" id="%s" value="%s" /></div>';
        $this->_format.='<div class="field-message">%s</div>';
        $this->_format.='</div>';
        
        $this->_error='<div class="error">%s</div>';
        
        $this->_locale=Zend_Registry::get('locale');
    }
    
    /**
     * Decorator render interface function
     *
     * @param string $content
     * @return string
     */
    public function render($content)
    {
        $element=$this->getElement();
        
        $type=$this->_getFieldTypeById($element->getType());
        
        $label=$this->_locale->_($element->getLabel());
        $id=$element->getId();
        $name=$element->getFullyQualifiedName();
        $value=$this->_locale->_($element->getValue());
        $message='';
        
        $messages=$element->getMessages();
        if (!empty($messages))
        {
            $message.='<ul>';
            foreach ($messages as $mess_code=>$mess_text)
            {
                $message.='<li>'.$this->_locale->_($this->_getMessageById($mess_code)).'</li>';
            }
            $message.='</ul>';
        }
        
        if (!empty($message))
        {
            $message=$this->_wrap_error($message);
        }
        
        return sprintf($this->_format,$id,$label,$type,$name,$id,$value,$message);
    }
    
    protected function _wrap_error($error)
    {
        return sprintf($this->_error,$error);
    }
    
    /**
     * Returns login form field types
     *
     * @return array
     */
    protected function _getFieldTypes()
    {
        return array(
            'Zend_Form_Element_Text' => 'text',
            'Zend_Form_Element_Password' => 'password',
            'Zend_Form_Element_Submit' => 'submit',
            'Zend_Form_Element_Hash' => 'hidden'
        );
    }
    
    /**
     * Returns login form default field type
     * Should never be used
     *
     * @return string
     */
    protected function _getDefaultFieldType()
    {
        $types=array_values($this->_getFieldTypes());
        
        return $types[0];
    }
    
    /**
     * Returns login form element type
     *
     * @param string $id
     * @return string
     */
    protected function _getFieldTypeById($id)
    {
        $types=$this->_getFieldTypes();
        
        if (array_key_exists($id,$types))
        {
            return $types[$id];
        } else 
        {
            return $this->_getDefaultFieldType();
        }
    }
    
    /**
     * Returns an array with login form messages
     *
     * @return array
     */
    protected function _getMessages()
    {
        return array(
                'isEmpty' => 'This field must be filled',
                'stringLengthTooShort' => 'Given value is too short',
                'stringLengthTooLong' => 'Given value is too long',
                'notSame' => 'Given hash do not match',
                'missingToken' => 'Hash not received or expired'
        );
    }
    
    /**
     * Returns default error message;
     * Should never be used
     *
     * @return string
     */
    protected function _getDefaultMessage()
    {
        return 'Invalid data';
    }
    
    /**
     * Returns message text by ID
     *
     * @param string $id
     * @return string
     */
    protected function _getMessageById($id)
    {
        $messages=$this->_getMessages();
        
        if (array_key_exists($id,$messages))
        {
            return $messages[$id];
        } else 
        {
            return $this->_getDefaultMessage();
        }
    }
}